package logic

import (
	"context"
	"errors"
	"goods/models/mysql"

	"goods/goodsrpc/goodsrpc"
	"goods/goodsrpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CollectDelLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCollectDelLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CollectDelLogic {
	return &CollectDelLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CollectDelLogic) CollectDel(in *goodsrpc.CollectDelRequest) (*goodsrpc.CollectDelResponse, error) {
	// todo: add your logic here and delete this line

	var user mysql.User
	tx := mysql.DB.Debug()
	tx.Begin()
	tx.Where("id=?", in.CommentId).Find(&user)
	if user.ID == 0 {
		tx.Rollback()
		return nil, errors.New("找不到该用户")

	}
	var collect mysql.Collect
	tx.Where("id=?", in.CommentId).Find(&collect)
	if collect.ID == 0 {
		tx.Rollback()
		return nil, errors.New("找不到该收藏夹")
	}
	tx.Delete(&collect)
	tx.Commit()
	return &goodsrpc.CollectDelResponse{}, nil
}
